package com.bz.xtcx.manager.mapper.provider;

import com.bz.xtcx.manager.entity.IndexVisiter;
import com.bz.xtcx.manager.persist.conditions.IndexVisiterCondition;
import com.bz.xtcx.manager.utils.WzDateUtil;
import com.bz.xtcx.manager.utils.WzStrUtil;
import org.apache.ibatis.jdbc.SQL;

public class IndexVisiterProvider {
    public String insertSelectiveProvider(IndexVisiter iv) {
        return new SQL() {
            {
                INSERT_INTO("index_visiter");
                VALUES("ip", "#{ip}");
                VALUES("visit_time", "#{visitTime}");
            }
        }.toString();
    }

    public String selectByConditionProvider(IndexVisiterCondition ivc) {
        StringBuilder sql = new StringBuilder("select DISTINCT ");
        sql.append("id, ");
        sql.append("ip, ");
        sql.append("visit_time ");
        sql.append("from index_visiter ");
        sql.append("where 1 = 1 ");
        if (null != ivc.getId()) {
            sql.append("and id = " + ivc.getId() + " ");
        }
        if (WzStrUtil.isNotBlank(ivc.getIp())) {
            sql.append("and ip = '" + ivc.getIp() + "' ");
        }
        if (null != ivc.getVisitTimeStart()) {
            sql.append("and visit_time >= '" + WzDateUtil.dt2Str(WzDateUtil.dayStart(ivc.getVisitTimeStart()), WzDateUtil.DT_FMT_5) + "' ");
        }
        if (null != ivc.getVisitTimeEnd()) {
            sql.append("and visit_time <= '" + WzDateUtil.dt2Str(WzDateUtil.dayEnd(ivc.getVisitTimeEnd()), WzDateUtil.DT_FMT_5) + "' ");
        }
        sql.append("order by visit_time desc ");
        if (ivc.getNeedPaging()) {
            sql.append("limit " + ivc.getPageBegin() + ", " + ivc.getPageSize());
        }
        return sql.toString();
    }

    public String countByConditionProvider(IndexVisiterCondition ivc) {
        StringBuilder sql = new StringBuilder("select count(DISTINCT id) ");
        sql.append("from index_visiter ");
        sql.append("where 1 = 1 ");
        if (null != ivc.getId()) {
            sql.append("and id = " + ivc.getId() + " ");
        }
        if (WzStrUtil.isNotBlank(ivc.getIp())) {
            sql.append("and ip = '" + ivc.getIp() + "' ");
        }
        if (null != ivc.getVisitTimeStart()) {
            sql.append("and visit_time >= '" + WzDateUtil.dt2Str(WzDateUtil.dayStart(ivc.getVisitTimeStart()), WzDateUtil.DT_FMT_5) + "' ");
        }
        if (null != ivc.getVisitTimeEnd()) {
            sql.append("and visit_time <= '" + WzDateUtil.dt2Str(WzDateUtil.dayEnd(ivc.getVisitTimeEnd()), WzDateUtil.DT_FMT_5) + "' ");
        }
        return sql.toString();
    }

    public String countWithIpByConditionProvider(IndexVisiterCondition ivc) {
        StringBuilder sql = new StringBuilder("select count(DISTINCT id) rec_count, ip ");
        sql.append("from index_visiter ");
        sql.append("where 1 = 1 ");
        if (null != ivc.getId()) {
            sql.append("and id = " + ivc.getId() + " ");
        }
        if (WzStrUtil.isNotBlank(ivc.getIp())) {
            sql.append("and ip = '" + ivc.getIp() + "' ");
        }
        if (null != ivc.getVisitTimeStart()) {
            sql.append("and visit_time >= '" + WzDateUtil.dt2Str(WzDateUtil.dayStart(ivc.getVisitTimeStart()), WzDateUtil.DT_FMT_5) + "' ");
        }
        if (null != ivc.getVisitTimeEnd()) {
            sql.append("and visit_time <= '" + WzDateUtil.dt2Str(WzDateUtil.dayEnd(ivc.getVisitTimeEnd()), WzDateUtil.DT_FMT_5) + "' ");
        }
        sql.append("group by ip");
        return sql.toString();
    }

    public String countWithIpAndDayByConditionProvider(IndexVisiterCondition ivc) {
        StringBuilder sql = new StringBuilder("select count(DISTINCT id) rec_count, ip ip, date_format(visit_time, '%Y%m%d') visit_time ");
        sql.append("from index_visiter ");
        sql.append("where 1 = 1 ");
        if (null != ivc.getId()) {
            sql.append("and id = " + ivc.getId() + " ");
        }
        if (WzStrUtil.isNotBlank(ivc.getIp())) {
            sql.append("and ip = '" + ivc.getIp() + "' ");
        }
        if (null != ivc.getVisitTimeStart()) {
            sql.append("and visit_time >= '" + WzDateUtil.dt2Str(WzDateUtil.dayStart(ivc.getVisitTimeStart()), WzDateUtil.DT_FMT_5) + "' ");
        }
        if (null != ivc.getVisitTimeEnd()) {
            sql.append("and visit_time <= '" + WzDateUtil.dt2Str(WzDateUtil.dayEnd(ivc.getVisitTimeEnd()), WzDateUtil.DT_FMT_5) + "' ");
        }
        sql.append("group by ip, date_format(visit_time, '%Y%m%d')");
        return sql.toString();
    }
}
